package edu.levantai;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.PopupMenu;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class ViewCountry extends Activity {
	protected TextView title;
	protected ImageView icon;

	private TextView nameTv;
	private TextView capTv;
	private TextView codeTv;

	private long rowID;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);

		setContentView(R.layout.view_country);

		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
				R.layout.window_title);
		
		

		title = (TextView) findViewById(R.id.lblTitle);
		icon = (ImageView) findViewById(R.id.imgIcon);

		this.title.setText(R.string.lblViewEditTitle);
		this.icon.setImageResource(R.drawable.icon_view_edit);

		setUpViews();

		Bundle extras = getIntent().getExtras();
		rowID = extras.getLong(CountryList.ROW_ID);
		
		TableLayout viewCountry = (TableLayout)findViewById(R.id.TableLayout1);
		registerForContextMenu(viewCountry);

	}

	private void setUpViews() {
		nameTv = (TextView) findViewById(R.id.nameText);
		capTv = (TextView) findViewById(R.id.capText);
		codeTv = (TextView) findViewById(R.id.codeText);
	}

	private class LoadContacts extends AsyncTask<Long, Object, Cursor> {
		DatabaseConnector dbConnector = new DatabaseConnector(ViewCountry.this);

		@Override
		protected Cursor doInBackground(Long... params) {
			dbConnector.open();
			return dbConnector.getOneContact(params[0]);
		}

		@Override
		protected void onPostExecute(Cursor result) {
			super.onPostExecute(result);

			result.moveToFirst();

			// get the column index for each data item
			int nameIndex = result.getColumnIndex("name");
			int capIndex = result.getColumnIndex("cap");
			int codeIndex = result.getColumnIndex("code");

			nameTv.setText(result.getString(nameIndex));
			capTv.setText(result.getString(capIndex));
			codeTv.setText(result.getString(codeIndex));

			result.close();
			dbConnector.close();

		}

	}

	protected void onResume() {
		super.onResume();
		new LoadContacts().execute(rowID);
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {		
		super.onCreateContextMenu(menu, v, menuInfo);
		
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.view_country_menu, menu);
		menu.setHeaderTitle("Select an option");
		menu.setHeaderIcon(R.drawable.select_option);		
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		
		switch (item.getItemId()) {
		case R.id.editItem:
			Intent intent = new Intent(ViewCountry.this, AddEditCountry.class);
			
			intent.putExtra(CountryList.ROW_ID, rowID);
			intent.putExtra("name", nameTv.getText());
			intent.putExtra("cap", capTv.getText());
			intent.putExtra("code", codeTv.getText());
			startActivity(intent);
			return true;
		case R.id.deleteItem:
			deleteContact();
			return true;
		default:
			return super.onContextItemSelected(item);
		}
		
	}
	
	private void deleteContact() {
		AlertDialog.Builder alert = new AlertDialog.Builder(ViewCountry.this);
		
		alert.setTitle(R.string.confirmTitle);
		alert.setMessage(R.string.confirmMessage);
		
		alert.setPositiveButton(R.string.delete_btn, new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				final DatabaseConnector dbConnector = new DatabaseConnector(ViewCountry.this);
				
				AsyncTask<Long, Object, Object> deleteTask = new AsyncTask<Long, Object, Object>() {

					@Override
					protected Object doInBackground(Long... params) {
						dbConnector.deleteContact(params[0]);
						return null;
					}

					@Override
					protected void onPostExecute(Object result) {
						finish();						
					}
					
					
					
					
					
				};
				
				deleteTask.execute(new Long[] {rowID});
				
			}
		});
		
		alert.setNegativeButton(R.string.cancel_btn, null).show();
		
	}
	
	
	
	
	
	

	
}
